﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Custom Convolution
    /// </summary>
    public partial class FormCustomConvolution : FormFilter
    {
        protected OwnOptimizedConvolutionInt m_Convolution = new OwnOptimizedConvolutionInt();
        /// <summary>
        /// Initializes a new instance of the <see cref="FormCustomConvolution"/> class.
        /// </summary>
        public FormCustomConvolution()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FormCustomConvolution_Shown);
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int nWidth = dataGridView1.Rows[0].Cells.Count;
            int nHeight = dataGridView1.RowCount;
            GetKernelFromDatagrid(nWidth, nHeight);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormCustomConvolution_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal = MainFormSingleton.ActivePictureBox.BackgroundImage;
            this.pictureBox1.BackgroundImage = m_ImageOriginal;
        }

        /// <summary>
        /// Handles the Click event of the loadToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the saveToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the cbOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            int nWidth = (int)nudWidth.Value;
            int nHeight = (int)nudHeight.Value;

            for (int i = 0; i < nWidth; i++)
            {
                dataGridView1.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()));
            }
            this.dataGridView1.Rows.Add(nHeight);
        }

        /// <summary>
        /// Handles the Click event of the cbCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void cbAll_Click(object sender, EventArgs e)
        {
            SetControlStates(true);
        }

        private void SetControlStates(bool _bChecked)
        {
            this.chkbA.Checked = _bChecked;
            this.chkbR.Checked = _bChecked;
            this.chkbG.Checked = _bChecked;
            this.chkbB.Checked = _bChecked;
        }

        private void cbNone_Click(object sender, EventArgs e)
        {
            SetControlStates(false);
        }

        private void cbInvert_Click(object sender, EventArgs e)
        {
            this.chkbA.Checked = !this.chkbA.Checked;
            this.chkbR.Checked = !this.chkbR.Checked;
            this.chkbG.Checked = !this.chkbG.Checked;
            this.chkbB.Checked = !this.chkbB.Checked;
        }

        private void cbApply_Click(object sender, EventArgs e)
        {
            int y = 0;
            int x = 0;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Rows.Clear();

            int nWidth = (int)nudWidth.Value;
            int nHeight = (int)nudHeight.Value;
            for (x = 0; x < nWidth; x++)
            {
                dataGridView1.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()));
                dataGridView1.Columns[x].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }

            for (y = 0; y < nHeight; y++)
            {
                dataGridView1.Rows.Add();
            }
            for (y = 0; y < dataGridView1.RowCount; y++)
            {
                for (x = 0; x < dataGridView1.Rows[y].Cells.Count;x++ )
                {
                    dataGridView1[x, y].Value = 0;
                }
            }
        }

        /// <summary>
        /// Get Kernel from Datagrid
        /// </summary>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        public void GetKernelFromDatagrid(int nWidth, int nHeight)
        {
            int[,] aKernel = new int[nWidth,nHeight];
            int x = 0;
            int y = 0;
            for (y = 0; y < dataGridView1.RowCount - 1; y++)
            {
                for (x = 0; x < dataGridView1.Rows[y].Cells.Count - 1; x++)
                {
                    object oValue = dataGridView1.Rows[y].Cells[x].Value;
                    if (oValue == null) oValue = 0; //normally we shouldn't get here...
                    aKernel[x, y] = (int)int.Parse(oValue.ToString(),CultureInfo.InvariantCulture);
                }
            }
            m_Convolution.Kernel = aKernel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbOK_Click_1(object sender, EventArgs e)
        {
            Image image = (Image)m_ImageOriginal.Clone();
            m_Convolution.Execute((Bitmap)image).Dispose();
            MainFormSingleton.ActivePictureBox.BackgroundImage = image;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
